# HTTP Sinker
# This module is responsible for sending the data to the HTTP endpoint:
# 1. read the json data from given jsonlines file name
# 2. send the data to the given HTTP endpoint which is configured in the config dict
import os
import jsonlines
import requests

from utils import LOG

class HttpSinker:
    def __init__(self, config: dict, common_paths: dict) -> None:
        self.common_paths = common_paths
        self.config = config
        self.sinker_url = config.get('sinker-url')
        LOG.debug(f"Sending data to {self.sinker_url}")

    def send_data(self):
        # read the json data from given jsonlines file name
        with jsonlines.open(os.path.join(self.common_paths.get('fake-data-output-files-path'), 'fake_data_gen.jsonl')) as reader:
            data = list(reader)
        # send the data to the given HTTP endpoint which is configured in the config dict
        for d in data:
            response = requests.post(self.sinker_url, json=d)
            LOG.debug(f"Response: status: {response.status_code}, msg: {response.text}")
